package com.ruoyi.system.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.imageio.ImageIO;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;

import static com.ruoyi.system.constant.Constants.PixelCount;

public class hasink {

    public static Map<String,Double> hasink_drawexcel(String papertype,String paperjson,String imagepth){
        Map<String,Double> map = new HashMap<>();
        JSONArray jsonArray = new JSONArray(paperjson);
        try {
            BufferedImage image = ImageIO.read(new File(imagepth));
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject point = jsonArray.getJSONObject(i);

                // 获取点的信息
                String id = point.getStr("id");
                int x = point.getInt("x");
                int y = point.getInt("y");
                int width = point.getInt("width");
                String type = point.getStr("type");
                if(type.equals("score")){
                    double score = point.getDouble("event");
                    String question = papertype+id;
                    if(!map.containsKey(question))
                        map.put(question,0.0);
                    System.out.println(hasInkInRegion(image, x, y, width, PixelCount));
                    if (hasInkInRegion(image, x, y, width, PixelCount)) {
                        map.put(question,score);
                    }
                }
            }
            return map;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }




    public static List<Integer> getint(String input){
        String[] integers = input.split("\\s+");
        List<String> intergerList = Arrays.asList(integers);
        List<Integer> intsList = new ArrayList<>();
        for (String s:intergerList){
            intsList.add(Integer.parseInt(s));
        }
        return intsList;
    }
    // 判断图像上的区域是否有笔迹信息的方法
    private static boolean hasInkInRegion(BufferedImage image, int x, int y, int width, int threshold) {
        int halfSquareWidth = width / 2;

        // 计算区域的左上角和右下角坐标
        int startX = Math.max(0, x - halfSquareWidth);
        int startY = Math.max(0, y - halfSquareWidth);
        int endX = Math.min(image.getWidth() - 1, x + halfSquareWidth);
        int endY = Math.min(image.getHeight() - 1, y + halfSquareWidth);

        int blackPixelCount = 0; // 记录黑色像素数量

        // 遍历区域内的像素
        for (int i = startX; i <= endX; i++) {
            for (int j = startY; j <= endY; j++) {
                // 获取像素颜色
                int pixel = image.getRGB(i, j);

                // 判断是否为黑色
                if ((pixel & 0x00FFFFFF) == 0x000000) {
                    blackPixelCount++; // 增加黑色像素数量
                }
            }
        }
        // 判断黑色像素数量是否超过阈值
        //System.out.println(x+"  "+y);
        //System.out.println(blackPixelCount);
        return blackPixelCount >= threshold;
    }


}
